VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "ApiKBDLLHOOKSTRUCT"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private Type KBDLLHOOKSTRUCT
    vkCode As Long
    scanCode As Long
    flags As Long
    time As Long
    dwExtraInfo As Long
End Type

Public CreatedOK As Boolean

'\\ Private memory handling functions
Private Declare Sub CopyMemoryKbdLLHookStruct Lib "kernel32" Alias "RtlMoveMemory" (Destination As KBDLLHOOKSTRUCT, ByVal Source As Long, ByVal Length As Long)
Private Declare Function IsBadReadPtrKbdLLHookStruct Lib "kernel32" Alias "IsBadReadPtr" (ByVal lp As Long, ByVal ucb As Long) As Long
Private Declare Function IsBadWritePtrKbdLLHookStruct Lib "kernel32" Alias "IsBadWritePtr" (ByVal lp As Long, ByVal ucb As Long) As Long

Public vkCode As Long
Public scanCode As Long
Private flags As Long
Public time As Long
Public dwExtraInfo As Long

Public Enum enLowLevelKeyboardFlagHelpers
    LLKHF_EXTENDED = &H1
    LLKHF_INJECTED = &H10
    LLKHF_ALTDOWN = &H20
    LLKHF_UP = &H80
End Enum

'\\ Additional functionality...
Private Declare Function GetKeyNameText Lib "user32" Alias "GetKeyNameTextA" (ByVal lParam As Long, ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" (ByVal cChar As Byte) As Integer

Public Property Get AltPressed() As Boolean

AltPressed = ((flags And LLKHF_ALTDOWN) = flags)
    
End Property


'\\ --[CreateFromPointer]---------------------------------------------
'\\ Fills this KbdLLHookStruct object from the location poiunted to by
'\\ lpKbdLLHookStruct
'\\ VB.NET Porting note: This function should be replaced with an override
'\\ of the New() for correctness
'\\ ----------------------------------------------------------------------------------------
'\\ (c) 2001 - Merrion Computing.  All rights  to use, reproduce or publish this code reserved
'\\ Please check http://www.merrioncomputing.com for updates.
'\\ ----------------------------------------------------------------------------------------
Friend Function CreateFromPointer(lpKbdLLHookStruct As Long) As Boolean

Dim ftThis As KBDLLHOOKSTRUCT

CreatedOK = False

If Not IsBadReadPtrKbdLLHookStruct(lpKbdLLHookStruct, Len(ftThis)) Then
    Call CopyMemoryKbdLLHookStruct(ftThis, lpKbdLLHookStruct, Len(ftThis))
    If Err.LastDllError = 0 Then
        With ftThis
            vkCode = .vkCode
            scanCode = .scanCode
            flags = .flags
            time = .time
            dwExtraInfo = .dwExtraInfo
        End With
        CreatedOK = True
    Else
        ReportError Err.LastDllError, "ApiKbdLLHookStruct:CreateFromPointer", GetLastSystemError
    End If
End If

CreateFromPointer = CreatedOK

End Function

Public Property Get ExtendedKey() As Boolean

ExtendedKey = ((flags And LLKHF_EXTENDED) = flags)

End Property

Public Property Get Injected() As Boolean

Injected = ((flags And LLKHF_INJECTED) = flags)

End Property


'\\ --[KeyNameText]-------------------------------------------------------------------------
'\\ Returns the name of the given virtual key e.g "Space" for
'\\ the spacebar.
'\\ ----------------------------------------------------------------------------------------
'\\ (c) 2001 - Merrion Computing.  All rights  to use, reproduce or publish this code reserved
'\\ Please check http://www.merrioncomputing.com for updates.
'\\ ----------------------------------------------------------------------------------------
Public Property Get KeyNameText() As String

Dim lRet As Long
Dim lParam As Long
Dim lpBuffer As String

'\\ Preinitialise the return string buffer
lpBuffer = String$(255, 0)

'0-15 blank, 16-23 vkCode, 24 - 31 blank
lParam = APIDispenser.MakeLong(0, scanCode)

lRet = GetKeyNameText(lParam, lpBuffer, Len(lpBuffer))
If Err.LastDllError > 0 Then
    ReportError Err.LastDllError, "ApiKBDLLHOOKSTRUCT:KeyNameText", GetLastSystemError
End If
If lRet > 0 Then
    lpBuffer = Left$(lpBuffer, lRet)
Else
    lpBuffer = ""
End If

KeyNameText = lpBuffer

End Property

Public Property Get KeyReleased() As Boolean

KeyReleased = ((flags And LLKHF_UP) = flags)

End Property


